package com.har.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.har.utils.http.HttpUtil;
import com.har.utils.security.B2BRSAUtil;
import com.har.utils.string.RandomUtils;

import java.util.Hashtable;
import java.util.Map;
import java.util.Random;


/**
 * 短信验证
 *
 * @author maorui
 *
 */
public class SendSingle {

    private static String postUrl = "https://y.huihua365.com/har-sms-front/api/sms/send/single";

    //生成6位验证码
    public static String genCodeNum(){
        //元素
        int[] array = {0,1,2,3,4,5,6,7,8,9};
        //随机对象
        Random rand = new Random();
        //循环产生
        for (int i = 10; i > 1; i--) {
            int index = rand.nextInt(i);
            int tmp = array[index];
            array[index] = array[i - 1];
            array[i - 1] = tmp;
        }
        //拼接结果为字符串
        int result = 0;
        for(int i = 0; i < 6; i++)
            result = result * 10 + array[i];
        String sixString =  Integer.toString(result);
        //有可能出现5位数，前面加0补全
        if (sixString.length() == 5) {
            sixString = "0" + sixString;
        }
        return sixString;
    }


    public static Map<String, String> send(String phone){
        Map<String,String> map=new Hashtable<>();
        int c = 0;
        for (int i = 0; i < 1; i++) {
            c = i+1;
//            System.out.println("当前第"+c+"个手机号："+phone);
            try {
                ReqMessage reqMessage = new ReqMessage();

                // 设置AppId
                reqMessage.setAppId("SMS000123");

                // 设置MsgKey
                String msgKey = RandomUtils.generateString(16);
                B2BRSAUtil b2bRSAUtil = new B2BRSAUtil();
                b2bRSAUtil.loadPublicKeyStr("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBTfsgQY2c3FoRdd8LEK5UWHs3mSYXPbuAW0vqB31yjP+EAfvANQjOZFOtDZCeAghowzhhb/JttCkOc/v3AabW9to+Vh6aASyn+tyDdMc4YXA1kezOhbr0VAbRllFEyBIoG++LT/7ac68419bTzZKia+gYV78QJv6PKj0J9Yl83wIDAQAB\n");
                reqMessage.setMsgKey(b2bRSAUtil.encryptStr(msgKey));

                JSONObject dataJson = new JSONObject();
                dataJson.put("phone_number", phone);
                //codePwd = genCodeNo();
                /**
                 * 【验证码：{0}，您正在使用汇安融金融服务云平台{1}，{2}分钟内有效。若非本人使用，请联系客服{3}。】
                 */
                String str=new String("【汇安融】您的验证码是");
                String  str1=new String("。如非本人操作，请忽略本短信");
                dataJson.put("content", str+genCodeNum()+str1);

                dataJson.put("opt_type", "138");
//				dataJson.put("content", "【交通银行】由于您信用良好，已符合我行信用卡金卡申请资格。最高5万，审批快，在线申请 http://dwz.cn/7QnZWZ，退订回T。");
//				dataJson.put("opt_type", "128");
                dataJson.put("extend", "1");
//				dataJson.put("callback_url", "1");
                System.out.println(dataJson.toJSONString());
                String data = AESUtil.getInstance(msgKey).encrypt(dataJson.toJSONString());
                reqMessage.setData(data);

//				b2bRSAUtil.loadPrivateKeyStr("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAOU6LtYx1Ud+QxKSYX8o0W0u5NjpBWdcIYqDkqnJiLiB5gM5UfA87E9TI1NWTaF/l6kvNAFEgx/1y8tPuB7AwxOualazwG9M7hji85K0tJfoS1+/hSipltYN4vf1pRTbxaGYkOfWraUl+U3MqyQUUh/jwJ87G+vD2bSmlFC4EgYHAgMBAAECgYBXB3oU1KK3HBb3DCB8GNE6dN1qBT6J+Y66ei3KzLtd4eOkQbT8UwUdMOFumIuszzaTg1ppSMvH7JhNPp0W//SP4ltywidDvasWqxlsv8CVlA2cn56laKjMAZ2hFrdd24/iHytG75V6mniKyHonNk/U2jCw9W04+xlL07USfon/cQJBAPjZ+6c3mkAzRk6biYPfoPs6xEoyurZx984rv2KPWxrOsJPyNLpV4z3gWbFsOZ6XsFDf6rZHWxcGuKRbPQ3CjXMCQQDrz+LnOlonTpAXxN6v1zwkE2tbXHXmUoWedN+DYTzclO+WX7ZFTr4kO40mCgPos9bUW9409/ljho0rgxHw2AAdAkAAnIB6w680UyvsC4PF+CmsFFPhHsjIo/UI46CfvFxr73ANnP22K3nYcBGoVuy5UAQV3sbz1a8KmnCfrr2yp/SzAkA7ftTZ0CAk4K58QMCKjvxugbPJdjeM5+8sfoJSLHtlj/Iu93EOZYKVs6y2FzrLQGyIYbFbneD/GliNzOD+6hCFAkBzbAxGVzUhicHOfK0PSZtM/mGaTkULmQL7gtOL6fm55pgVLpRrJoshyzhi5fCho7RuRrjUIfMBG8uNdIPZG9BS");  // 合作机构RSA私钥签名
//				String sign = b2bRSAUtil.makeSign(dataJson.toJSONString());
//				reqMessage.setSign(sign);
                //--------------------------------------以上代码为封装请求报文---------------------------------------------------

                // 请求返回
                String resp = HttpUtil.post(postUrl, reqMessage.getReqMessage().toJSONString());

                System.out.println("resp:" + resp);

                JSONObject respJson = JSONObject.parseObject(resp);
                // 解密返回参数
                String dataString = respJson.getString("Data");
//				b2bRSAUtil.loadPrivateKeyStr("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAOU6LtYx1Ud+QxKSYX8o0W0u5NjpBWdcIYqDkqnJiLiB5gM5UfA87E9TI1NWTaF/l6kvNAFEgx/1y8tPuB7AwxOualazwG9M7hji85K0tJfoS1+/hSipltYN4vf1pRTbxaGYkOfWraUl+U3MqyQUUh/jwJ87G+vD2bSmlFC4EgYHAgMBAAECgYBXB3oU1KK3HBb3DCB8GNE6dN1qBT6J+Y66ei3KzLtd4eOkQbT8UwUdMOFumIuszzaTg1ppSMvH7JhNPp0W//SP4ltywidDvasWqxlsv8CVlA2cn56laKjMAZ2hFrdd24/iHytG75V6mniKyHonNk/U2jCw9W04+xlL07USfon/cQJBAPjZ+6c3mkAzRk6biYPfoPs6xEoyurZx984rv2KPWxrOsJPyNLpV4z3gWbFsOZ6XsFDf6rZHWxcGuKRbPQ3CjXMCQQDrz+LnOlonTpAXxN6v1zwkE2tbXHXmUoWedN+DYTzclO+WX7ZFTr4kO40mCgPos9bUW9409/ljho0rgxHw2AAdAkAAnIB6w680UyvsC4PF+CmsFFPhHsjIo/UI46CfvFxr73ANnP22K3nYcBGoVuy5UAQV3sbz1a8KmnCfrr2yp/SzAkA7ftTZ0CAk4K58QMCKjvxugbPJdjeM5+8sfoJSLHtlj/Iu93EOZYKVs6y2FzrLQGyIYbFbneD/GliNzOD+6hCFAkBzbAxGVzUhicHOfK0PSZtM/mGaTkULmQL7gtOL6fm55pgVLpRrJoshyzhi5fCho7RuRrjUIfMBG8uNdIPZG9BS");
//				String msgKeyString = b2bRSAUtil.decryptStr(respJson.getString("MsgKey"));
//				String msgKeyString = b2bRSAUtil.decryptStr(msgKey);

                String respData = AESUtil.getInstance(msgKey).decrypt(dataString);
//				FileUtil.appendFile("D:/code/smsresult.txt", i + "	" + respData);

                System.out.println("data:" + respData);
                map.put("data",dataJson.get("content").toString());
                map.put("resp",resp);
                map.put("respData",respData);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
        return map;
    }
    public static Map<String, String> sendCommon(String phone){
        Map<String,String> map=new Hashtable<>();
        int c = 0;
        for (int i = 0; i < 1; i++) {
            c = i+1;
//            System.out.println("当前第"+c+"个手机号："+phone);
            try {
                ReqMessage reqMessage = new ReqMessage();

                // 设置AppId
                reqMessage.setAppId("SMS000123");

                // 设置MsgKey
                String msgKey = RandomUtils.generateString(16);
                B2BRSAUtil b2bRSAUtil = new B2BRSAUtil();
                b2bRSAUtil.loadPublicKeyStr("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCBTfsgQY2c3FoRdd8LEK5UWHs3mSYXPbuAW0vqB31yjP+EAfvANQjOZFOtDZCeAghowzhhb/JttCkOc/v3AabW9to+Vh6aASyn+tyDdMc4YXA1kezOhbr0VAbRllFEyBIoG++LT/7ac68419bTzZKia+gYV78QJv6PKj0J9Yl83wIDAQAB\n");
                reqMessage.setMsgKey(b2bRSAUtil.encryptStr(msgKey));

                JSONObject dataJson = new JSONObject();
                dataJson.put("phone_number", phone);
                //codePwd = genCodeNo();
                /**
                 *
                 * 【汇安融】验证码：{0}，您正在使用汇安融金融服务云平台{1}，{2}分钟内有效。若非本人使用，请联系客服{3}
                 */
                //拼接短信模板
                StringBuffer sb=new StringBuffer();
                sb.append("【汇安融】验证码：");
                sb.append(genCodeNum());
                sb.append("，您正在使用汇安融金融服务云平台");
                sb.append("");
                sb.append("，");
                sb.append(Const.MIN);
                sb.append("分钟内有效。若非本人使用，请联系客服");
                sb.append(Const.TELEPHONE);
                sb.append("。");

                dataJson.put("content", sb);
                dataJson.put("opt_type", "152");
                dataJson.put("extend", "1");
                System.out.println(dataJson.toJSONString());
                String data = AESUtil.getInstance(msgKey).encrypt(dataJson.toJSONString());
                reqMessage.setData(data);
                //--------------------------------------以上代码为封装请求报文---------------------------------------------------
                // 请求返回
                String resp = HttpUtil.post(postUrl, reqMessage.getReqMessage().toJSONString());
                System.out.println("resp:" + resp);

                JSONObject respJson = JSONObject.parseObject(resp);
                // 解密返回参数
                String dataString = respJson.getString("Data");
                String respData = AESUtil.getInstance(msgKey).decrypt(dataString);

                System.out.println("data:" + respData);
                map.put("data",dataJson.get("content").toString());
                map.put("resp",resp);
                map.put("respData",respData);
            } catch (Exception e) {

                e.printStackTrace();
            }
        }
        return map;
    }
}
